﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace physio.assist
{
    /// <summary>
    /// Interaction logic for Aniversarios.xaml
    /// </summary>
    public partial class Aniversarios : Window
    {
        public Aniversarios()
        {
            InitializeComponent();



            //Ajusta posição e tamanho das janelas
            double width = System.Windows.SystemParameters.PrimaryScreenWidth;
            double height = System.Windows.SystemParameters.PrimaryScreenHeight;

            winAniversarios.Width = width;
            winAniversarios.Height = height;

            winAniversarios.Left = 0;
            winAniversarios.Top = 0;




            //Ajusta posicionamento e tamanho dos panels
            pnlGeral.Width = width;
            pnlGeral.Height = height;

            pnlVoltar.Height = height;
            pnlPrimaria.Height = height;
            //pnlSecundaria.Height = height;

            pnlVoltar.Width = width * 0.2;
            pnlPrimaria.Width = width * 0.8;
            //pnlSecundaria.Width = width * 0.3;






            //Lista aniversários do dia
            ListaAniversarios(DateTime.Now.Day, DateTime.Now.Month);
            InsereEspaco();
            ListaAniversarios(DateTime.Now.AddDays(1).Day, DateTime.Now.AddDays(1).Month);
            InsereEspaco();
            ListaAniversarios(DateTime.Now.AddDays(2).Day, DateTime.Now.AddDays(2).Month);




        }







        //ANIVERSARIOS
        private void ListaAniversarios(int dia, int mes)
        {

            PhysioAssistWS.PhysioAssistSoapClient ws = new PhysioAssistWS.PhysioAssistSoapClient();

            foreach (PhysioAssistWS.Aniversario aniversario in ws.ListaAniversario(dia, mes))
            {

                InsereAniversario(dia, mes, aniversario.Nome);

            }

        }
        private void InsereAniversario(int dia, int mes, string nome)
        {

            //Cria o panel
            StackPanel pnl = new StackPanel();
            pnl.Orientation = Orientation.Horizontal;
            pnl.VerticalAlignment = System.Windows.VerticalAlignment.Bottom;

            //Cria imagem do bolo
            Image img = new Image();
            img.Source = new BitmapImage(new Uri(@"pack://application:,,,/physio.assist;component/Images/bolo.png"));

            //Cria label da data
            Label lblData = new Label();
            lblData.Content = dia + "/" + mes;
            lblData.FontSize = 25;
            lblData.Foreground = Brushes.White;

            //Cria label do nome
            Label lblNome = new Label();
            // lblNome.FontWeight = FontWeights.Bold;
            lblNome.FontSize = 25;
            lblNome.Content = nome;

            //Insere componente no panel
            //pnl.Children.Add(img);
            pnl.Children.Add(lblData);
            pnl.Children.Add(lblNome);

            //Insere panel no panel de aniversarios
            pnlPrimaria.Children.Add(pnl);

        }
        private void InsereEspaco()
        {
            Label lbl = new Label();
            lbl.Content = "\n";
            pnlPrimaria.Children.Add(lbl);
        }







        //RECONHECIMENTO DE VOZ
        private Microsoft.Speech.Recognition.SpeechRecognitionEngine speechEngine;
        private Microsoft.Kinect.KinectSensor sensor;
        private void winAgenda_Loaded(object sender, RoutedEventArgs e)
        {


            //Obtem o sensor ativo
            foreach (var potentialSensor in Microsoft.Kinect.KinectSensor.KinectSensors)
            {
                if (potentialSensor.Status == Microsoft.Kinect.KinectStatus.Connected)
                {
                    sensor = potentialSensor;
                    break;
                }
            }


            if (null != sensor)
            {

                try
                {
                    sensor.Start();
                }
                catch (System.IO.IOException)
                {
                    sensor = null;
                }
            }








            //RECONHECIMENTO DE VOZ
            Microsoft.Speech.Recognition.RecognizerInfo ri = GetKinectRecognizer();

            if (null != ri)
            {

                this.speechEngine = new Microsoft.Speech.Recognition.SpeechRecognitionEngine(ri.Id);

                // Create a grammar from grammar definition XML file.
                using (var memoryStream = new System.IO.MemoryStream(Encoding.ASCII.GetBytes(Properties.Resources.PhysioAssistGrammar)))
                {
                    var g = new Microsoft.Speech.Recognition.Grammar(memoryStream);
                    speechEngine.LoadGrammar(g);
                }

                speechEngine.SpeechRecognized += SpeechRecognized;

                speechEngine.SetInputToAudioStream(sensor.AudioSource.Start(), new Microsoft.Speech.AudioFormat.SpeechAudioFormatInfo(Microsoft.Speech.AudioFormat.EncodingFormat.Pcm, 16000, 16, 1, 32000, 2, null));
                speechEngine.RecognizeAsync(Microsoft.Speech.Recognition.RecognizeMode.Multiple);
            }


        }
        private static Microsoft.Speech.Recognition.RecognizerInfo GetKinectRecognizer()
        {
            foreach (Microsoft.Speech.Recognition.RecognizerInfo recognizer in Microsoft.Speech.Recognition.SpeechRecognitionEngine.InstalledRecognizers())
            {
                string value;
                recognizer.AdditionalInfo.TryGetValue("Kinect", out value);
                if ("True".Equals(value, StringComparison.OrdinalIgnoreCase) && "en-US".Equals(recognizer.Culture.Name, StringComparison.OrdinalIgnoreCase))
                {
                    return recognizer;
                }
            }

            return null;
        }
        private void SpeechRecognized(object sender, Microsoft.Speech.Recognition.SpeechRecognizedEventArgs e)
        {
            // Speech utterance confidence below which we treat speech as if it hadn't been heard
            const double ConfidenceThreshold = 0.3;

            if (e.Result.Confidence >= ConfidenceThreshold)
            {
                switch (e.Result.Semantics.Value.ToString())
                {
                    case "CLOSE":
                        winAniversarios.Close();
                        break;

                    case "FECHAR":
                        winAniversarios.Close();
                        break;
                }
            }
        }






    }
}
